package com.yunxi.poc.starbucks.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunxi.poc.starbucks.common.BaseController;
import com.yunxi.poc.starbucks.common.BizConstant;
import com.yunxi.poc.starbucks.entity.TStoreCapacity;
import com.yunxi.poc.starbucks.service.ITStoreCapacityService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author shiyuan.xlc
 * @since 2021-08-12
 */
@Controller
@RequestMapping("/starbucks/store_capacity")
public class TStoreCapacityController extends BaseController {

    @Resource
    ITStoreCapacityService storeCapacityService;

    @ResponseBody
    @RequestMapping(value = {""}, method = RequestMethod.GET)
    public ResponseEntity list(@RequestParam("storeId") String storeId, @RequestParam("orderDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate orderDate) {
        if (!StringUtils.hasLength(storeId)) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("0");
        }

        QueryWrapper<TStoreCapacity> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("business_day", orderDate.minusDays(BizConstant.SCROWDED_EFFICIENCY_DAYS));
        queryWrapper.lt("business_day", orderDate);
        queryWrapper.eq("store_id", storeId);
        List<TStoreCapacity> orderNewDetailsList = storeCapacityService.list(queryWrapper);

        orderNewDetailsList.sort(Comparator.comparing(TStoreCapacity::getBusinessDay));
        return ResponseEntity.status(HttpStatus.OK).body(orderNewDetailsList);
    }
}
